package com.quyeying.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName TokenUtil
 * @Description TODO
 * @Author skx
 * @Date 2021/9/22
 */
@Component
public class TokenUtil {

    private static RedisUtil redisUtil;

    @Autowired
    RedisUtil autowiredRedisUtil;

    @PostConstruct
    public void initRedisUtil() {
        redisUtil = autowiredRedisUtil;
    }

    /**
     * 获取当前用户id
     */
    public static long getUserId(HttpServletRequest request) {

        //获取当前用户id
        String authorization = request.getHeader("Authorization");
        long userId = Long.parseLong(redisUtil.get(authorization).toString());
        return userId;
    }

    /**
     * 验证token是否有效
     */
    public static Boolean verfiyToken(HttpServletRequest request) {

        //获取当前用户id
        String authorization = request.getHeader("Authorization");
        if(ObjectUtils.isEmpty(authorization) || ObjectUtils.isEmpty(redisUtil.get(authorization))) {
            return false;
        }
        return true;
    }
}
